<!-- <script src="__PUBLIC__/js/bootstrap3-typeahead.min.js"></script> -->
 <script src="http://cdn.bootcss.com/holder/2.7.1/holder.js"></script>
<script type="text/javascript">
    $("#product-image").bind("click",function(){
      $("#fileToUpload").click();
    });
    function fileSelected(fileToUpload,target,id) {
        var file = document.getElementById(fileToUpload).files[0];
        if (file) {
            $(".progress").show();
            var fd = new FormData();
            fd.append("fileToUpload", document.getElementById(fileToUpload).files[0]);
            fd.append("target",target);
            fd.append("id",id);
            var xhr = new XMLHttpRequest();
            xhr.upload.addEventListener("progress", uploadProgress, false);
            xhr.addEventListener("load", uploadComplete, false);
            xhr.addEventListener("error", uploadFailed, false);
            xhr.addEventListener("abort", uploadCanceled, false);
            xhr.open("POST", "__APP__/Upload/user");//修改成自己的接口
            xhr.send(fd);
        }
    }

    function uploadProgress(evt) {
        if (evt.lengthComputable) {
            var percentComplete = Math.round(evt.loaded * 100 / evt.total);
            //document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
            $(".progress-bar").css("width",function(){return percentComplete.toString() + '%'});
            $(".progress-bar").text(percentComplete.toString() + '%');
        }else {
            document.getElementById('progressNumber').innerHTML = 'unable to compute';
        }
    }
        
    function uploadComplete(evt) {
        /* 服务器端返回响应时候触发event事件*/
        //alert(evt.target.responseText);
        var res= JSON.parse(evt.target.responseText);
        if(res.status==1){
            $('#'+res.target).attr("src", res.imgurl); 
            alert('上传成功');
            $(".progress").hide();
        }else{
            alert(res.msg);
        }
    }
    function uploadFailed(evt) {
        alert("There was an error attempting to upload the file.");
    }
    function uploadCanceled(evt) {
        alert("The upload has been canceled by the user or the browser dropped the connection.");
    }

    function address2coordinator(){

        var address=$.trim($("input[name='address']").val());
        if(!address){
                $("input[name='address']").parent().addClass('has-error');
                $("input[name='address']").attr("placeholder","地址不能为空");
                return;            
        }

        $.post("__APP__/Lbs/getaddress",{address:address},function(data){
            if(data.status==1){
                $("input[name='province']").val(data.province);
                $("input[name='city']").val(data.city);
                $("input[name='district']").val(data.district);
                if($("input[name='district']").val()==""){
                    $("input[name='district']").val(data.city);
                }
            }else{
                alert("输入的地址有误，请写填完整");
                return;
            }

        });
    }
</script>
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header"><i class="fa fa-chevron-left" onclick="closeurl();"></i> 医生管理</h1>
                </div>
                <!-- /.col-lg-12 -->
            </div>



            <!-- /.row -->
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            编辑医生 (上传头像尺寸:200x200)
                        </div>
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                                <form role="form">
<!--                                     <div class="row">
                                        <div class="col-lg-9">
                                            <div class="form-group">
                                                <label>医生名称 </label>
                                                <input class="form-control" name="name" value="{$doctor.name}">
                                            </div>
                                        </div>
                                    </div> -->
                                    <div class="row">
                                        <div class="col-lg-12">
                                            <div class="col-lg-3">
<!--                                                 <center>
                                                    <img id="product-image" class="img-thumbnail" alt="200x200" src="<if condition="$doctor.image eq ''">{$doctor.headimgurl}<else />{$doctor.image}</if>" width="200" height="200" ><input type="file" name="fileToUpload" accept="image/*" id="fileToUpload" onchange="fileSelected('fileToUpload','product-image',1);" style="display:none;"/>
                                                    <div class="progress" style="display:none;width:200px">
                                                        <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
                                                            60%
                                                        </div>
                                                    </div>
                                                </center> -->
                                                <center>
                                                    <img id="product-image" class="img-thumbnail" alt="200x200" data-src="holder.js/200x200"><input type="file" name="fileToUpload" accept="image/*" id="fileToUpload" onchange="fileSelected('fileToUpload','product-image',1);" style="display:none;"/>
                                                    <div class="progress" style="display:none;width:200px">
                                                        <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
                                                            60%
                                                        </div>
                                                    </div>
                                                </center>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('name')}</label>
                                                            <input class="form-control" name="name">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('gender')}</label>
                                                            <select class="form-control" name="sex">
                                                                <option value="1" selected="selected">男</option>
                                                                <option value="2">女</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('hospital')}</label>
                                                            <input class="form-control" name="hospital">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('address')}</label>
                                                            <input class="form-control" name="address">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">                                                
                                                        <div class="form-group">
                                                            <label>{:L('province')}  <button type="button" class="btn btn-success btn-xs" onclick="address2coordinator()">点击该按钮可通过医院地址获取医生当前所在省/直辖市</button></label>
                                                            <input class="form-control" name="province" readonly="readonly">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('department')}</label>
                                                            <!-- <input class="form-control" name="department" value="{$doctor.department}"> -->
                                                            <select class="form-control" name="department">
                                                                <option value="">--- 请选择 ---</option>
                                                                <volist name="department" id="vo">
                                                                <option value="{$vo.name}">{$vo.name}</option>
                                                                </volist>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('position')}</label>
                                                            <input class="form-control" name="position">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('socialposition')}</label>
                                                            <input class="form-control" name="socialposition">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('mobile')}</label>
                                                            <input class="form-control" name="mobile">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('email')}</label>
                                                            <input class="form-control" type="email" name="email">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('pinyin')}</label>
                                                            <input class="form-control" name="pinyin">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('resume')}</label>
                                                            <textarea class="form-control" rows="4" name="resume"></textarea>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group" style="display:none">
                                                            <label>{:L('city')}</label>
                                                            <input class="form-control" name="city" readonly="readonly">
                                                        </div>  
                                                        <div class="form-group" style="display:none">
                                                            <label>{:L('district')}</label>
                                                            <input class="form-control" name="district" readonly="readonly">
                                                        </div>                          
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('status')}</label>
                                                            <select class="form-control" name="status">
                                                                <option value="1" selected="selected">正常</option>
                                                                <option value="2">停用</option>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>{:L('level')}</label>
                                                            <select class="form-control" name="level">
                                                                <option value="">--- 请选择 ---</option>
                                                                <volist name="level" id="vo">
                                                                <option value="{$vo.id}">{$vo.level}</option>    
                                                                </volist>
                                                            </select>
                                                        </div>
                                                    </div>
                                                </div>
<!--                                                 <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>单份病例价格</label>
                                                            <input class="form-control" name="recordprice" value="{$doctor.recordprice}">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>订阅价格</label>
                                                            <input class="form-control" name="subprice" value="{$doctor.subprice}">
                                                        </div>
                                                    </div>
                                                </div>   
                                                <div class="row">
                                                    <div class="col-lg-12">
                                                        <div class="form-group">
                                                            <label>邀请评论价格</label>
                                                            <input class="form-control" name="discussprice" value="{$doctor.discussprice}">
                                                        </div>
                                                    </div>
                                                </div>                                              
                                            </div> -->
                                        </div>
                                    </div>
                                    <center>
                                        <button type="button" class="btn btn-success" onclick="check()">提交</button>
                                        <button type="button" class="btn btn-danger" onclick="closeurl();">关闭</button>
                                        <!-- <button type="button" class="btn btn-danger" onclick="gourl('__APP__/Doctor');">关闭</button> -->
                                    </center>
                                    </form>
                            <!-- /.table-responsive -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->

        </div>

    <!-- Page-Level Demo Scripts - Tables - Use for reference -->
    <script>
        // $("input[name='hospital']").on('input',function(){
        //     var key=$("input[name='hospital']").val();
        //     if(!key){
        //         return;
        //     }
        //     $.post('__APP__/Hospital/search', {key:key},function(data){
        //         $("input[name='hospital']").typeahead({ source:data,autoSelect: true});
        //     });
        // });


        function check(){
            var name = $.trim($("input[name='name']").val());
            var image= $("#product-image").attr('src');
            var hospital = $.trim($("input[name='hospital']").val());
            var department = $.trim($("select[name='department']").val());
            var position = $.trim($("input[name='position']").val());                          
            var socialposition = $.trim($("input[name='socialposition']").val());
            var mobile = $.trim($("input[name='mobile']").val());
            var email = $.trim($("input[name='email']").val());
            var pinyin = $.trim($("input[name='pinyin']").val());
            var resume = $.trim($("textarea[name='resume']").val());
            var address = $.trim($("input[name='address']").val());
            var province = $.trim($("input[name='province']").val());
            var city = $.trim($("input[name='city']").val());
            var district = $.trim($("input[name='district']").val());
            var sex = $.trim($("select[name='sex']").val());
            var status = $.trim($("select[name='status']").val());
            var level = $.trim($("select[name='level']").val());

            if(!address){
                $("input[name='address']").parent().addClass('has-error');
                return;
            }
            if(!province){
                $("input[name='province']").parent().addClass('has-error');
                return;
            }
            if(!city){
                $("input[name='city']").parent().addClass('has-error');
                return;
            }
            if(!district){
                $("input[name='district']").parent().addClass('has-error');
                return;
            }

            if(!name){
                $("input[name='name']").parent().addClass('has-error');
                return;
            }
            if(!hospital){
                $("input[name='hospital']").parent().addClass('has-error');
                return;
            }
            if(!department){
                $("select[name='department']").parent().addClass('has-error');
                return;
            }
            if(!position){
                $("input[name='position']").parent().addClass('has-error');
                return;
            }                        
            if(!socialposition){
                $("input[name='socialposition']").parent().addClass('has-error');
                return;
            }
            if(!mobile || isNaN(mobile) || !/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(mobile)){
                $("input[name='mobile']").parent().addClass('has-error');
                return;
            }
            if(!email){
                $("input[name='email']").parent().addClass('has-error');
                return;
            }
            if(!pinyin || !isNaN(pinyin) || pinyin.length>1){
                $("input[name='pinyin']").parent().addClass('has-error');
                return;
            }                  
            if(!resume){
                $("textarea[name='resume']").parent().addClass('has-error');
                return;
            }
            if(!sex || isNaN(sex)){
                $("select[name='sex']").parent().addClass('has-error');
                return;
            }
            if(!status || isNaN(status)){
                $("select[name='status']").parent().addClass('has-error');
                return;
            }
            if(!level || isNaN(level)){
                $("select[name='status']").parent().addClass('has-error');
                return;
            }
            $.post('__APP__/User/add',
                    {
                    name:name,
                    image:image,
                    hospital:hospital,
                    address:address,
                    province:province,
                    city:city,
                    district:district,
                    department:department,
                    position:position,
                    socialposition:socialposition,
                    mobile:mobile,
                    email:email,
                    pinyin:pinyin,
                    resume:resume,
                    sex:sex,
                    status:status,
                    level:level,
                    },
                function(data){
                    if(data.status==1){
                        alert('操作成功');
                        gourl('__APP__/User/index');
                    }else{
                        alert('操作失败');
                    }
            });

        }
    </script>

</body>

</html>
